package com.itqf.controller;

import com.itqf.entity.TbItem;
import com.itqf.service.TbItemService;
import com.itqf.service.impl.TbItemServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/22
 * @Time: 下午3:06
 */
@Controller
public class TbItemController {

    //TbItemController --springmvc容器
    //service--- spring容器
    //子容器可以访问父容器内容
    @Autowired
    private TbItemService tbItemService;

    //日志  分析程序的运行流程
    private Logger logger = Logger.getLogger(TbItemServiceImpl.class);



    @RequestMapping("/findAll")
    //jsp页面  String
    //html  异步请求  List<>
    @ResponseBody
    public List<TbItem>  findAll(){

        return  tbItemService.findAll();

    }

    @RequestMapping(value = "/doAdd",produces ="text/html;charset=utf-8")
    public  String  doAdd(MultipartFile image, TbItem tbItem, HttpServletRequest request){
        try {
            //1.得到文件名
            String filename = image.getOriginalFilename();
            logger.info("【新增的商品的图片名】"+filename);
            //得到存储目录
            String path  = request.getServletContext().getRealPath("/upload");
            logger.info("【新增的商品的图片存储路径】"+path);
            File dir = new File(path);
            if (!dir.exists()){
                dir.mkdir();
            }
            //文件后缀
            String  suffix = filename.substring(filename.lastIndexOf("."));
            logger.info("【新增的商品的图片的后缀】"+suffix);

            filename = System.currentTimeMillis()+suffix;

            image.transferTo(new File(dir,filename));

            tbItem.setImages("/upload/"+filename);

            int i = tbItemService.addTbItem(tbItem);

            if (i>0){
                //防止表单重复提交
                return  "redirect:index.html";
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return "add.html";

    }

    @RequestMapping("/doUpdate")
    public  String  doUpdate(MultipartFile image, TbItem tbItem, HttpServletRequest request){
        try {
            //1.得到文件名
            String filename = image.getOriginalFilename();
            logger.info("【修改的商品的图片名】"+filename);
            //得到存储目录
            String path  = request.getServletContext().getRealPath("/upload");
            logger.info("【修改的商品的图片存储路径】"+path);
            File dir = new File(path);
            if (!dir.exists()){
                dir.mkdir();
            }
            //文件后缀
            String  suffix = filename.substring(filename.lastIndexOf("."));
            logger.info("【修改的商品的图片的后缀】"+suffix);

            filename = System.currentTimeMillis()+suffix;

            image.transferTo(new File(dir,filename));

            tbItem.setImages("/upload/"+filename);

            int i = tbItemService.update(tbItem);

            if (i>0){
                //防止表单重复提交
                return  "redirect:index.html";
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/toUpdate/"+tbItem.getId();

    }

    @RequestMapping("/toUpdate/{id}")
    public  String toUpdate(@PathVariable long id, Model model){
        //int  q = 151236582637540;
        TbItem tbItem =   tbItemService.findById(id);
        model.addAttribute("tbItem",tbItem);

        return  "/update.jsp";


    }

    @RequestMapping("/del")
    //   /del/1
    //   /del   [1,2,3,4]
    @ResponseBody
    public Map<String,Object> del(@RequestBody List<Long> ids){

        int i = tbItemService.batchDelete(ids);
        Map<String,Object> map = new HashMap<>();
        if (i>0){
            map.put("code",0);
        }else{
            map.put("code",1);
            map.put("msg","批量删除失败");
        }


        return  map;
    }


}
